<?php

namespace App\Http\Controllers\Api;


use App\Http\Controllers\Controller;
use App\Http\Requests\client\PreTakeRequest;
use App\Models\PreTakeModel;
use App\Models\PreGoodsModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class PreTake extends Controller
{
    use PreTakeRequest;

    /**
     * Notes:列表
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request) {
        $auth = $request->get('auth');
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 20);
        $status = $request->input('status', '');

        $query = PreTakeModel::query()
            ->with(['province', 'city', 'district'])->where('driver_id', $auth->id);


        if($status){
            $query = $query->where('status', $status);
        }


        $total = $query->count('id');
        $items = $query
            ->offset(($page-1) * $limit)
            ->limit($limit)
            ->orderBy('id', 'desc')
            ->get();

        $list = $items->map(function($item) {
            $volume = 0;
            $weight = 0;
            if($item->goods){
                foreach ($item->goods as $goods) {
                    $volume += $goods->volume;
                    $weight += $goods->weight;
                }
            }
            $res = [
                'id' => $item->id,
                'contact' => $item->contact,
                'mobile' => $item->mobile,
                'pre_time' => $item->pre_time,
                'province' => $item->province->name ?? '',
                'city' => $item->city->name ?? '',
                'district' => $item->district->name ?? '',
                'address' => $item->address,
                'goods_volume' => $volume,
                'goods_weight' => $weight,
            ];
            return $res;
        });

        return $this->success(['list' => $list, 'total' => $total]);
    }

    /**
     * Notes:司机接单
     * User: xiangzi
     * Date: 2024/9/29
     * Time: 11:31
     * @return \Illuminate\Http\JsonResponse
     */
    public function accept(Request $request, $id)
    {
        $model = PreTakeModel::find($id);

        if (!$model) {
            return $this->error('数据不存在');
        }

        $model->status = 3;
        $model->driver_time = date('Y-m-d H:i:s');
        $model->save();

        return $this->success();
    }

    /**
     * Notes:取件完成
     * User: xiangzi
     * Date: 2024/9/29
     * Time: 11:33
     * @return \Illuminate\Http\JsonResponse
     */
    public function complete(Request $request, $id){
        $model = PreTakeModel::find($id);

        if (!$model) {
            return $this->error('数据不存在');
        }

        $model->status = 4;
        $model->take_time = date('Y-m-d H:i:s');
        $model->save();

        return $this->success();
    }

    /**
     * Notes:取件失败
     * User: xiangzi
     * Date: 2024/9/29
     * Time: 11:33
     * @return \Illuminate\Http\JsonResponse
     */
    public function fail(Request $request, $id){
        $params = $request->all();

        $model = PreTakeModel::find($id);

        if (!$model) {
            return $this->error('数据不存在');
        }

        $model->status = 5;
        $model->take_remarks = $params['take_remarks'] ?? '';
        $model->fail_time = date('Y-m-d H:i:s');
        $model->save();

        return $this->success();
    }

    /**
     * Notes:送达仓库
     * User: xiangzi
     * Date: 2024/9/29
     * Time: 11:33
     * @return \Illuminate\Http\JsonResponse
     */
    public function warehouse(Request $request, $id){
        $model = PreTakeModel::find($id);

        if (!$model) {
            return $this->error('数据不存在');
        }

        $model->status = 6;
        $model->warehouse_time = date('Y-m-d H:i:s');
        $model->save();

        return $this->success();
    }

    /**
     * Notes:详情
     * User: xiangzi
     * Date: 2024/9/29
     * Time: 11:31
     * @return \Illuminate\Http\JsonResponse
     */
    public function detail(Request $request)
    {
        $id = $request->input('id', 0);

        $model = PreTakeModel::query()->with('goods')
            ->with(['province', 'city', 'district', 'car', 'driver'])->find($id);

        if (!$model) {
            return $this->error('数据不存在');
        }

        $volume = 0; $weight = 0;
        if($model->goods){
            foreach ($model->goods as $goods) {
                $volume += $goods->volume;
                $weight += $goods->weight;
            }
        }

        $return = [
            'pre_info' => [
                'id' => $model->id,
                'contact' => $model->contact,
                'company' => $model->company,
                'mobile' => $model->mobile,
                'pre_time' => $model->pre_time,
                'count' => $model->goods ? count($model->goods) : 0,
                'volume' => $volume,
                'weight' => $weight,
                'remarks' => $model->remarks
            ]
        ];

        $return['log_info'] = [];

        if($model->driver){
            $return['log_info'][] = [
                'key' => '司机',
                'val' => $model->driver->name ?? '',
            ];

            $return['log_info'][] = [
                'key' => '司机电话',
                'val' => $model->driver->mobile ?? '',
            ];
        }

        if($model->car){
            $return['log_info'][] = [
                'key' => '车牌',
                'val' => $model->car->car_number ?? '',
            ];
        }

        if($model->driver_time){
            $return['log_info'][] = [
                'key' => '开始取件',
                'val' => $model->driver_time
            ];
        }

        if($model->fail_time){
            $return['log_info'][] = [
                'key' => '取件失败',
                'val' => $model->fail_time
            ];
        }

        if($model->take_remarks){
            $return['log_info'][] = [
                'key' => '失败原因',
                'val' => $model->take_remarks
            ];
        }

        if($model->take_time){
            $return['log_info'][] = [
                'key' => '取件时间',
                'val' => $model->take_time
            ];
        }

        if($model->warehouse_time){
            $return['log_info'][] = [
                'key' => '送达仓库',
                'val' => $model->warehouse_time
            ];
        }

        if($model->is_read == 0){
            $model->is_read = 1;
            $model->save();
        }

        return $this->success($return);
    }



}
